package qlsl.androiddesign.view.subview.commonview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import qlsl.androiddesign.activity.commonactivity.MemberCenterActivity;
import qlsl.androiddesign.activity.commonactivity.MemberInfoActivity;
import qlsl.androiddesign.activity.commonactivity.MemberLoginActivity;
import qlsl.androiddesign.activity.commonactivity.ToolSettingActivity;
import qlsl.androiddesign.activity.subactivity.BusinessOrderActivity;
import qlsl.androiddesign.activity.subactivity.CarCollectActivity;
import qlsl.androiddesign.activity.subactivity.CollectionActivity;
import qlsl.androiddesign.activity.subactivity.ExChangeActivity;
import qlsl.androiddesign.activity.subactivity.MemberProductActivity;
import qlsl.androiddesign.activity.subactivity.MemberPublishActivity;
import qlsl.androiddesign.activity.subactivity.MemberRecruitActivity;
import qlsl.androiddesign.activity.subactivity.MessageCenterActivity;
import qlsl.androiddesign.activity.subactivity.MyPostActivity;
import qlsl.androiddesign.activity.subactivity.PostingReplyActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingCarActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingOrderActivity;
import qlsl.androiddesign.activity.subactivity.SignActivity;
import qlsl.androiddesign.adapter.commonadapter.MemberCenterAdapter;
import qlsl.androiddesign.manager.ActivityManager;
import qlsl.androiddesign.method.UserMethod;
import qlsl.androiddesign.util.commonutil.ActivityUtils;
import qlsl.androiddesign.util.commonutil.DialogUtil;
import qlsl.androiddesign.util.commonutil.ImageUtils;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 个人中心页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class MemberCenterView extends FunctionView<MemberCenterActivity> {

	private ListView listView;

	public MemberCenterView(MemberCenterActivity activity) {
		super(activity);
		setContentView(R.layout.activity_member_center);
	}

	protected void initView(View view) {
		setTitle("个人中心");
		setRightButtonResource(R.drawable.registration);
		showRightButton();
		listView = (ListView) findViewById(R.id.listView);
	}

	public void onResume() {
		setMemberInfo();
	}

	protected void initData() {
		setListViewData();
	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_right:
			startActivity(SignActivity.class);
			break;
		case R.id.list_item:
			doClickListItem(view);
			break;
		case R.id.btn_exit:
			doClickExitView();
			break;
		}
	}

	private void setMemberInfo() {
		String name = UserMethod.getUser().getUsername();
		String photoUrl = UserMethod.getUser().getPhoto();
		TextView tv_name = findViewById(R.id.tv_name);
		ImageView iv_photo = findViewById(R.id.photoView);
		tv_name.setText(name);
		ImageUtils.circle(activity, photoUrl, iv_photo);
	}

	private void setListViewData() {
		Integer[] icons = new Integer[] { null, R.drawable.personal_data, R.drawable.personal_message, null,
				R.drawable.personal_order, R.drawable.personal_car, R.drawable.personal_collection, null,
				R.drawable.personal_posted, R.drawable.personal_reply, R.drawable.personal_replyp, null,
				R.drawable.personal_released, R.drawable.personal_collection1, null, R.drawable.personal_exchange,
				R.drawable.personal_service, null, R.drawable.personal_data, R.drawable.personal_data, null,
				R.drawable.personal_released };
		String[] names = new String[] { "个人信息", "个人资料", "消息中心", "商城", "我的订单", "购物车", "我的收藏", "论坛", "我的发帖", "回复我的",
				"招聘求职", "二手车市场", "我的发布", "设备收藏", "个人中心", "兑换订单", "联系客服", "个人设置", "个性化设置", "下载新版本", "商家中心", "我的商品" };
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		int length = names.length;
		if (!UserMethod.isBusiness()) {
			length = length - 2;
		}
		for (int index = 0; index < length; index++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("icon", icons[index]);
			map.put("text", names[index]);
			list.add(map);
		}
		MemberCenterAdapter adapter = new MemberCenterAdapter(activity, list);
		listView.setAdapter(adapter);
	}

	private void doClickListItem(View view) {
		int position = listView.getPositionForView(view);
		if (position == 1) {
			startActivity(MemberInfoActivity.class);
		} else if (position == 2) {
			startActivity(MessageCenterActivity.class);
		} else if (position == 4) {
			if (UserMethod.isBusiness()) {
				startActivity(BusinessOrderActivity.class);
			} else {
				startActivity(ShoppingOrderActivity.class);
			}
		} else if (position == 5) {
			startActivity(ShoppingCarActivity.class);
		} else if (position == 6) {
			startActivity(CollectionActivity.class);
		} else if (position == 8) {
			startActivity(MyPostActivity.class);
		} else if (position == 9) {
			startActivity(PostingReplyActivity.class);
		} else if (position == 10) {
			startActivity(MemberRecruitActivity.class);
		} else if (position == 12) {
			startActivity(MemberPublishActivity.class);
		} else if (position == 13) {
			startActivity(CarCollectActivity.class);
		} else if (position == 15) {
			startActivity(ExChangeActivity.class);
		} else if (position == 16) {
			ActivityUtils.callNum(activity, "02783268968");
		} else if (position == 18) {
			startActivity(ToolSettingActivity.class);
		} else if (position == 19) {
			launchAppDetail(activity.getPackageName(), "");
		} else if (position == 21) {
			startActivity(MemberProductActivity.class);
		}
	}

	private void doClickExitView() {
		DialogUtil.showDialog(this, "注销登录", "确定退出当前登录用户?");
	}

	public void onClick(DialogInterface dialog, int which) {
		if (which == -1) {
			UserMethod.setUser(null);
			UserMethod.setToken(null);
			UserMethod.setRole(null);
			ActivityManager.getInstance().popAllActivity();
			startActivity(MemberLoginActivity.class);
		}
	}

	/**
	 * 启动到app详情界面,本市场没有时，会联网查询其详情<br/>
	 *
	 * @param appPkg
	 *            App的包名
	 * @param marketPkg
	 *            应用商店包名 ,如果为""则由系统弹出应用商店列表供用户选择,否则调转到目标市场的应用详情界面，某些应用商店可能会失败
	 */
	private void launchAppDetail(String appPkg, String marketPkg) {
		try {
			if (TextUtils.isEmpty(appPkg))
				return;
			Uri uri = Uri.parse("market://details?id=" + appPkg);
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			if (!TextUtils.isEmpty(marketPkg))
				intent.setPackage(marketPkg);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
			showToast("未检测到您手机安装任何应用市场");
		}
	}

}
